from django.shortcuts import render,redirect

from django.http import HttpResponse,JsonResponse

from ..models import *

from django.contrib.auth.decorators import permission_required
from django.core.urlresolvers import reverse



# order列表
@permission_required('myadmin.show_order',raise_exception = True)
def orderlist(request):
    # 获取所有的商品数据
    orderlist = Order.objects.all()
    # 分配数据
    context = {'orderlist':orderlist}
    # 加载模板
    return render(request,'myadmin/order/index.html',context)

def orderinfo(request,oid):
    # 获取所有的商品数据和收货信息
    orderinfo = OrderInfo.objects.filter(orderid=oid)
    orderrecv=Order.objects.get(id=oid)
    # 分配数据
    context={'orderinfo':orderinfo,'orderrecv':orderrecv}
    # 记载数据、
    return render(request,'myadmin/order/orderinfo.html',context)


def orderedit(request,oid):
    # 判断方式
    if request.method=='GET':
        ob= Order.objects.get(id=oid)
        context={'ob':ob}
        return render(request,'myadmin/order/edit.html',context)
    elif request.method == 'POST':
        data=request.POST.dict()
        data.pop('csrfmiddlewaretoken')
        ob=Order.objects.get(id=oid)
        ob.status=data['status']
        ob.save()
        return HttpResponse('<script>alert("状态修改成功");location.href="'+reverse('myadmin_order_list')+'"</script>')








